package piwan;

import java.util.ArrayList;

import javax.media.opengl.GL;

public class HighRenderer {
	
	public HighRenderer(MainWindow win, World w) {
		this.win = win;
		this.w = w;
		
		cols = new Colour[this.w.getRegionCount()];
		regs = new ArrayList<Region>();
		gl = this.win.getGL();
	}
	
	void setRenderOwners( boolean b ) { doOwners = b; }
	
	
	void addHighlight( Region r, Colour c ) {
		if( cols[r.getId()] == null )
			regs.add(r);
		
		cols[r.getId()] = c;
	}
	
	void removeHighlight( Region r ) {
		if( cols[r.getId()] != null ) {
			cols[r.getId()] = null;
			regs.remove(r);
		}
	}
	
	public void render() {
		gl.glDisable(GL.GL_LIGHTING);
		gl.glDisable(GL.GL_DEPTH_TEST);
		gl.glDisable(GL.GL_TEXTURE_2D);
		gl.glEnable(GL.GL_BLEND);
		//gl.glLineWidth(6.0f);
		if(doOwners)
			for(Region r: w.getRegions()) {
				if(cols[r.getId()]==null) {
					if(r.isOwned())
						doHighlight(r,r.getOwner().getColour());
				} else
					doHighlight(r,cols[r.getId()]);
			}
		else
			for(Region r: regs) {
				if(cols[r.getId()]!=null)
					doHighlight(r,cols[r.getId()]);
			}
	}
	
	private void doHighlight(Region r, Colour c) {
		
		gl.glColor4d(c.r, c.g, c.b, 1.0);
		
		gl.glBegin(GL.GL_QUADS);
		for(Polygon p: r.getPolygons())
			p.quadRender(gl, 0.00001, 0.02);//p.lineRender(gl,0.0);
		gl.glEnd();
		
		/*for(Polygon p: r.getPolygons())
			p.lineRender(gl, 0.00001);
		//gl.glDisable(GL.GL_BLEND);*/
	}
	
	
	private MainWindow	win = null;
	private World		w = null;
	private GL			gl = null;
	private Colour[]	cols = null;
	private boolean		doOwners = true;
	private ArrayList<Region> regs = null;
}
